package com.practicesuite.controllayer;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing patient health over time.
 * 
 * @author James
 */
public class ViewMedicalHistoryPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JTextField textField;
    public MainWindow window;
    
    private List<TreatmentRecord> medicalHistory;
    private List<Date> dates;
    private int[] weights;
    
    double numPatients;
    double billingAmount;

    /**
     * Create the panel.
     */
    public ViewMedicalHistoryPanel(MainWindow container, Patient patient, final User user) {
        setPreferredSize(new Dimension(500, 400));
        setLayout(new BorderLayout(0, 0));
        window = container;
        
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 100));
        add(panel, BorderLayout.NORTH);
        panel.setLayout(new GridLayout(0, 1, 0, 0));
        
        JLabel lblViewPatientHealth = new JLabel("Patient Health Histories");
        lblViewPatientHealth.setHorizontalTextPosition(SwingConstants.CENTER);
        lblViewPatientHealth.setHorizontalAlignment(SwingConstants.CENTER);
        lblViewPatientHealth.setFont(new Font("Tahoma", Font.PLAIN, 24));
        panel.add(lblViewPatientHealth);
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1);
        
        textField = new JTextField();
        panel_1.add(textField);
        textField.setColumns(15);
        
        JButton btnSearchPatients = new JButton("Search Patients");
        btnSearchPatients.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    //TODO what happens if patient is not found?
                    Patient newPatient = SystemDriver.getPatient(textField.getText());
                    System.out.println(newPatient.toString());
                    System.out.println(user.toString());
                    int[] newArray = new int[2];
                    newArray[0] = 0; newArray[1] = 0;
                    System.out.println(newArray);
                    window.refresh("viewMedicalHistory", newPatient, null, user);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, contact administrator.");
                    e.printStackTrace();
                }
            }
        });
        btnSearchPatients.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_1.add(btnSearchPatients);
        
        JPanel panel_2 = new JPanel();
        panel_2.setPreferredSize(new Dimension(500, 50));
        add(panel_2, BorderLayout.SOUTH);
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_2.add(btnReturnToHome);
        
        JPanel panel_3 = new JPanel();
        add(panel_3, BorderLayout.CENTER);
        
        if (patient != null) {
            try {
                /*medicalHistory = new ArrayList<TreatmentRecord>();
                Date dateTime1 = new Date();
                TreatmentRecord treatmentRecord1 = new TreatmentRecord(21, dateTime1, 31, 
                        "Fever", new int[] {1, 2, 3, 4, 5}, "Drink water", null);
                Date dateTime2 = new Date();
                TreatmentRecord treatmentRecord2 = new TreatmentRecord(22, dateTime2, 32, 
                        "Headache", new int[] {1, 2, 3, 4, 5}, "Take aspirin", null);
                Date dateTime3 = new Date();
                TreatmentRecord treatmentRecord3 = new TreatmentRecord(23, dateTime3, 33, 
                        "Naseau", new int[] {1, 2, 3, 4, 5}, "Eat tums", null);
                Date dateTime4 = new Date();
                TreatmentRecord treatmentRecord4 = new TreatmentRecord(24, dateTime4, 34, 
                        "Heartburn", new int[] {1, 2, 3, 4, 5}, "Pepto bismol", null);
                medicalHistory.add(treatmentRecord1);
                medicalHistory.add(treatmentRecord2);
                medicalHistory.add(treatmentRecord3);
                medicalHistory.add(treatmentRecord4);*/
                //medicalHistory = patient.getMedicalHistory();
                medicalHistory = SystemDriver.getTreatmentRecordList(patient);
                int length = medicalHistory.size();
                dates = new ArrayList<Date>();
                weights = new int[length];
                
                for (int i = 0; i < length; i++) {
                    int[] vitalSigns = medicalHistory.get(i).getVitalSigns();
                    weights[i] = vitalSigns[4];
                    dates.add(medicalHistory.get(i).getDateTime());
                }
                
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                for (int i = 0; i < length; i++) {
                    dataset.setValue(weights[i], "Weight", dates.get(i).toString());
                }
                JFreeChart chart = ChartFactory.createBarChart3D("Health History",
                        "Date", "Weight", dataset, PlotOrientation.VERTICAL, false,
                        true, false);
                
                try {
                    ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 450, 350);
                    
                    System.out.println("Chart created");
                } catch (IOException e)
                  {
                    System.err.println("Problem occurred creating chart.");
                  }
                
                JLabel label_2 = new JLabel();
                label_2.setIcon(new ImageIcon("chart.jpg"));
                panel_3.add(label_2);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                e.printStackTrace();
            }
        }

    }

}
